require "digest"

class User < ActiveRecord::Base

  SALT_PREFIX = "aslfhnaclnl&^GKHNgnfkaKYUUSGYDAON&N^R"
  SALT_SUFFIX = "KNV@V^%$B&$N*^N*%^*N%^^%VCSACGCVU$WSR"

  validates :email, :name, :presence => true
  validates_length_of :name, :within => 4..40
  validates_length_of :password, :within => 4..40
  validates_uniqueness_of :email


  has_many :games

  def authenticate()
    return false if email.nil? || password.nil?
    find_by_email_and_password(email, password)
  end


  def hashed_password
    password.to_s
  end

  def hashed_password=(pass)
    password = generate_hash(pass)
  end

  def self.generate_hash(password)
    slated_password = SALT_PREFIX + password + SALT_SUFFIX
    Digest::MD5.hexdigest(salted_password)
  end
end
